module.exports = app => {
	const Sequelize = app.Sequelize;
	const project = app.model.define('projects', {
		id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
		name: {type: Sequelize.STRING(255), allowNull: false, defaultValue: '', comment: '项目名称'},
		parent_id: {type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '父ID'},
		manager_id: {type: Sequelize.INTEGER, allowNull: false, comment: '管理者ID'},
		project_template_id: {type: Sequelize.INTEGER, allowNull: false, comment: '创建时使用的模板ID'},
		progress: {type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '项目进度0-100'},
		cover: {type: Sequelize.STRING(255), allowNull: false, defaultValue: '', comment: '项目封面'},
		is_private: {type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '是否为私有项目'},
		is_recycle: {type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '是否进入回收站'},
		is_archived: {type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '是否已归档'},
		is_auto_progress: {type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '是否自动更新项目进度'},
		state: {type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '项目状态'},
		intro: {type: Sequelize.STRING(255), allowNull: false, defaultValue: '', comment: '简介'},
	})
	project.associate = function() {
		// 项目属于一个管理者（多对一关系）
		project.belongsTo(app.model.Users, {
			foreignKey: 'manager_id',
			as: 'creator'
		});

		// 项目有多个成员（多对多关系）
		project.belongsToMany(app.model.Users, {
			through: app.model.UserProjects,
			foreignKey: 'project_id',
			otherKey: 'user_id',
			as: 'member',  // 改为单数，匹配查询中的使用
		});

		// 项目收藏者（多对多关系）
		project.belongsToMany(app.model.Users, {
			through: app.model.UserProjectCollects,
			foreignKey: 'project_id',
			otherKey: 'user_id',
			as: 'collector',
		});
	}
	return project;
}